<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" %>
<%@include file="/quick/include/common.jsp"%>
<%@include file="/quick/include/ajaxRichGrid.jsp"%>
<%@include file="/quick/include/commonQuery.jsp"%>
<c:set var="buttons">
    <input type="button" style="margin-right: 5px"   onclick="f_add('T_GRADE_MAJOR')" value="<fmt:message key="button.add"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_update('T_GRADE_MAJOR')" value="<fmt:message key="button.update"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_delete('T_GRADE_MAJOR')" value="<fmt:message key="button.delete"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_cancel('T_GRADE_MAJOR')" value="<fmt:message key="button.cancel"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_save('T_GRADE_MAJOR')" value="<fmt:message key="button.save"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_query('T_GRADE_MAJOR')" value="<fmt:message key="button.query"/>"/>
</c:set>
<div id='toolbar' style="display:none">
<c:out value="${buttons}" escapeXml="false"/>
</div>
<div id="parameter_form" align=center></div> 

<c:set var="buttons">
	<input type="button" style="margin-right: 5px" onclick="f_add('T_MAJOR_DIRECTION')"		value="<fmt:message key="button.add"/>" />
	<input type="button" style="margin-right: 5px" onclick="f_update('T_MAJOR_DIRECTION')"		value="<fmt:message key="button.update"/>" />
	<input type="button" style="margin-right: 5px" onclick="f_delete('T_MAJOR_DIRECTION')"		value="<fmt:message key="button.delete"/>" />
	<input type="button" style="margin-right: 5px" onclick="f_cancel('T_MAJOR_DIRECTION')"		value="<fmt:message key="button.cancel"/>" />
</c:set>
<div id='subToolbar1' style="display:none">
	<c:out value="${buttons}" escapeXml="false" />
</div>
<c:set var="buttons">
	<input type="button" style="margin-right: 5px" onclick="f_add('T_MAJOR_CLASS')"		value="<fmt:message key="button.add"/>" />
	<input type="button" style="margin-right: 5px" onclick="f_update('T_MAJOR_CLASS')"		value="<fmt:message key="button.update"/>" />
	<input type="button" style="margin-right: 5px" onclick="f_delete('T_MAJOR_CLASS')"		value="<fmt:message key="button.delete"/>" />
	<input type="button" style="margin-right: 5px" onclick="f_cancel('T_MAJOR_CLASS')"		value="<fmt:message key="button.cancel"/>" />
</c:set>
<div id='subToolbar2' style="display:none">
	<c:out value="${buttons}" escapeXml="false" />
</div>
<c:set var="buttons">
	<input type="button" style="margin-right: 5px" onclick="f_add_course('V_MAJOR_DEGREE_COURSE')"  type='add'		value="<fmt:message key="button.add"/>" />
	<input type="button" style="margin-right: 5px" onclick="f_delete('V_MAJOR_DEGREE_COURSE')"		value="<fmt:message key="button.delete"/>" />
	<input type="button" style="margin-right: 5px" onclick="f_cancel('V_MAJOR_DEGREE_COURSE')"		value="<fmt:message key="button.cancel"/>" />
</c:set>
<div id='subToolbar3' style="display:none">
	<c:out value="${buttons}" escapeXml="false" />
</div>
<div id='subToolbar' style="display:none"></div>

<script>
var form,cascadeWin,collegeGrid;
var ycTable='T_GRADE_MAJOR';
var refid,schoolYear;
Ext.onReady(globalScope._ready);

Ext.onReady(function(){
  Ext.get('title').dom.style.display='none';
	var yearStore=new Ext.data.SimpleStore({
     'fields':['value','text'],
     data:<c:out value="${model.yearJson}" escapeXml='false' />
	});
	var yearCollegeStore=new Ext.data.SimpleStore({
     'fields':['value','text','group'],
     data:<c:out value="${model.yearCollegeJson}" escapeXml='false' />
	});
     
	form = new Ext.form.FormPanel({
	     el:'parameter_form',
  		labelAlign:'right',
	    frame:true,
	    border:true,
	    height:40,
	    items:[{
	          layout:'column',
	          items:[{
	            layout:'form',
	            columnWidth:1/4,
	            items:[{
			            xtype:'combo',   
			            fieldLabel:'年级',  
			            store: yearStore,   
			            editable: false,   
			            mode: 'local',   
			            hiddenName:'GRADE',   
			            id:'_GRADE',   
			            displayField:'text',   
			            valueField:'value',
			            triggerAction: 'all', 
			            emptyText:'请选择',   
			            width:200         
			         }
	         ]},{columnWidth:1/4},{
	           layout:'form',
	           columnWidth:1/4,
	           items:[
			         {
			            xtype:'combo',   
			            fieldLabel:'系',   
			            store: yearCollegeStore,   
			            mode: 'local',   
			            hiddenName:'YEAR_COLLEGR',   
			            id:'_YEAR_COLLEGR',   
			            displayField:'text',   
			            valueField:'value',   
			            triggerAction: 'all',   
			            emptyText:'请选择',   
			            width:200         
			         }
            ]}
          ]  
         }
       ]  
	});
   form.render();

    var gradeCombo=form.getForm().findField('_GRADE');
    var yearCollegeCombo=form.getForm().findField('_YEAR_COLLEGR');
    yearCollegeCombo.lastQuery="";
	 
	 gradeCombo.on('change',function(combo){
	   yearCollegeCombo.reset();
	 });
	 
	 yearCollegeCombo.on('focus',function(combo){	
	    var grade=gradeCombo.getValue();
	    yearCollegeCombo.store.filterBy(function(r){
	      if(r.get('group')==grade||r.get('group')=='null') return true;
	    }); 
	 });    

	yearCollegeCombo.on('select',function(comboBox){
	    refid=comboBox.getValue();
	    schoolYear=gradeCombo.getValue();
	    globalScope._model({tableName:ycTable,REFID:refid,GRADE:schoolYear},function(responseText){
	        var htmlDiv=Ext.get("ajaxTable_html").dom;
		    htmlDiv.innerHTML=responseText;
		    var htmlTable=Ext.get(ycTable);
		    globalScope.grid.get(ycTable).reloadData(htmlTable);	
	   },false);	
	}); 
  
});


Ext.onReady(function(){
  globalScope.grid.get(ycTable).on('rowdblclick',f_cascadeUpdate_class);
  var contextmenu = new Ext.menu.Menu({
  	id:'theContextMenu',
  	items:[{
  		text:'专业方向',
  		handler:function(){
          f_cascadeUpdate_direction();
  		}},
  		{
  		text:'行政班级',
  		handler:function(){
          f_cascadeUpdate_class();
  		}},
  		{
  		text:'学位课程',
  		handler:function(){
          f_cascadeUpdate_course();
  		}}]
  });
  globalScope.grid.get(ycTable).on('rowcontextmenu',function(grid,i,e){
    grid.getSelectionModel().selectRow(i);
  	e.preventDefault();
  	contextmenu.showAt(e.getXY());
  });
    
});


function f_cascadeUpdate_direction(){
  var mainTable=ycTable;
  var subTable='T_MAJOR_DIRECTION';
  var subToolbar=Ext.get('subToolbar');
  var subToolbar1=Ext.get('subToolbar1');
  subToolbar.dom.innerHTML=subToolbar1.dom.innerHTML;
  globalScope._cascadeUpdate(mainTable,subTable);
}
function f_cascadeUpdate_class(){
  var mainTable=ycTable;
  var subTable='T_MAJOR_CLASS';
  var subToolbar=Ext.get('subToolbar');
  var subToolbar2=Ext.get('subToolbar2');
  subToolbar.dom.innerHTML=subToolbar2.dom.innerHTML;
  globalScope._cascadeUpdate(mainTable,subTable);
}
function f_cascadeUpdate_course(){
  var mainTable=ycTable;
  var subTable='V_MAJOR_DEGREE_COURSE';
  var subToolbar=Ext.get('subToolbar');
  var subToolbar3=Ext.get('subToolbar3');
  subToolbar.dom.innerHTML=subToolbar3.dom.innerHTML;
  var grid=globalScope.grid.get(mainTable);
  var r=grid.getSelectionModel().getSelected();
  globalScope._cascadeUpdate(mainTable,subTable,{tableName:subTable,MAJOR_ID:r.id});
}



function f_majorDiretion(){
  var r=globalScope.grid.get(ycTable).getSelectionModel().getSelected();
  if(Ext.isEmpty(r)){
    Ext.Msg.alert('WARNING','请选择专业');
    return;
  }
  var id=r.id;
   var matrix=[["tableName","T_MAJOR_DIRECTION"],["REFID",id]];
   var form=createForm("<c:url value='/quick/quickWithParentInfo.html'/>",matrix);
   form.submit();
}
function f_majorClass(){
  var r=globalScope.grid.get(ycTable).getSelectionModel().getSelected();
  if(Ext.isEmpty(r)){
    Ext.Msg.alert('WARNING','请选择专业');
    return;
  }
  var id=r.id;
   var matrix=[["tableName","T_MAJOR_CLASS"],["REFID",id]];
   var form=createForm("<c:url value='/quick/quickWithParentInfo.html'/>",matrix);
   form.submit();
}

function f_add_course(tableName){
  f_commonQuery("T_DEGREE_COURSE",function(recs){
	    Ext.each(recs,function(item){
		      var commonQueryGrid=globalScope.grid.get(tableName);
		      var commonStore=commonQueryGrid.getStore();
			  var newRecord=commonQueryGrid.newRecord('newId');//新建Record
			  newRecord.copyData(item.data); //复制数据
			  newRecord.set('COURSE_ID',item.id);
			  commonQueryGrid.stopEditing();//插入Record
	    	  commonStore.insert(0,newRecord); 
	    	  commonQueryGrid.startEditing(0,0);
	    	  newRecord.dirty=true;
	    	  newRecord.modified=newRecord.data;
	    	  if(commonStore.modified.indexOf(newRecord)==-1){
	    	    commonStore.modified.push(newRecord);	
	    	  }	
	    });	
  });
}

    function i_cascadeAdd(mainTable,subTable,mainColumns,subColumns,form,rec){
      if(subTable!="V_MAJOR_DEGREE_COURSE"){
	      form.getForm().updateRecord(rec);
	      globalScope._save(mainTable,'quick/quickJsonProcess.html',false,null,false);
	      globalScope._save(subTable,'quick/quickJsonProcess.html',true,{REFID:rec.id},false);
	      return true;
      }else{
	      form.getForm().updateRecord(rec);
	      globalScope._save(mainTable,'quick/quickJsonProcess.html',false,null,false);
	      globalScope._save("V_MAJOR_DEGREE_COURSE",'quick/quickJsonProcess.html',true,{MAJOR_ID:rec.id},false,"T_MAJOR_DEGREE_COURSE");
	      return true;
      }
    }

    function i_cascadeUpdate(mainTable,subTable,mainColumns,subColumns,form,rec){
      i_cascadeAdd(mainTable,subTable,mainColumns,subColumns,form,rec);
      return true;
    }


function f_add(tableName){
  globalScope._add(tableName);
}

function f_update(tableName){
  globalScope._update(tableName);
}

function f_delete(tableName){
  globalScope._delete(tableName);
} 

function f_cancel(tableName){
  globalScope._cancel(tableName);
}

function f_save(tableName,url){
  if(Ext.isEmpty(refid)){
    Ext.Msg.alert('','请选择系');
  }
  if(Ext.isEmpty(schoolYear)){
    Ext.Msg.alert('','请选择学年度');
  }
  globalScope._simpleSave({talbeName:tableName,url:'quick/quickJsonProcess.html',saveTableName:'T_GRADE_MAJOR',postData:{REFID:refid,GRADE:schoolYear}});
}

function f_query(tableName){
    globalScope._query(tableName);
}
function f_gotoPage(pageNo,rowsOfPage,pageCount,rowCount,conditoin){
  globalScope._gotoPage({spanName:"g_pageSpan_<c:out value='${model.tableName}'/>",pageNo:pageNo,rowsOfPage:rowsOfPage,pageCount:pageCount,rowCount:rowCount,condition:conditoin,tableName:"<c:out value='${model.tableName}'/>"});
}
</script>
